package com.onLineCar.file.service;

import com.alibaba.nacos.common.utils.IoUtils;
import com.aliyun.oss.OSS;
import com.onLineCar.file.config.OssConfig;
import com.onLineCar.file.utils.FileUploadUtils;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * Oss 文件存储
 *
 * @author onLineCar
 */
@Primary
@Service
public class OssSysFileServiceImpl implements ISysFileService {
    @Autowired
    private OssConfig ossConfig;

    @Autowired
    private OSS ossClient;

    /**
     * Oss文件上传接口
     *
     * @param file 上传的文件
     *
     * @return 访问地址
     *
     * @throws Exception
     */
    @Override
    public String uploadFile (MultipartFile file) throws Exception {

        String filename = file.getOriginalFilename();
        String fileSuf = filename.substring(filename.indexOf(".") + 1);
        LocalDateTime now = LocalDateTime.now();
        String url =
                now.getYear() + "/" +
                        now.getMonth() + "/" +
                        now.getDayOfMonth() + "/" +
                        now.getHour() + "/" +
                        now.getMinute() + "/";
        String ossPath = url + UUID.randomUUID().toString() + "." + fileSuf;
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ossClient.putObject(ossConfig.getBucketName(), ossPath, inputStream);
        return ossConfig.getAccessPre() + ossPath;
    }




}
